<?php
 class ContactsController extends AppController {
 	var $name = 'Contacts';
	var $uses = array('Contact','Infor');
 	function beforeFilter() {
		parent::beforeFilter();

		//$this->set('mod','contacts');
		$this->Auth->allow(array('resources'));
	}
	function index() {
		if (!empty($this->data)) {
			$this->Contact->create();
			if ($this->Contact->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu', true));
				$this->redirect(array('controller'=>'news','action' => 'home'));
			} else {
				$this->Session->setFlash(__('Chưa lưu được. Hãy thử lại.', true));
			}
		}
		$contacts = $this->Infor->find('first',array('fields'=>array('content'),'conditions'=>array('area'=>'contact','lang'=>$this->lg)));
		$maps = $this->Infor->find('first',array('fields'=>array('content'),'conditions'=>array('area'=>'other')));
		$this->set(compact('contacts','maps'));
	}
	function admin_index() {

		$this->Contact->recursive = 0;
		$conditions = null;
		$key = null;
		$stt = null;
		$key = $this->data['Contact']['key'];
		$stt = $this->data['Contact']['status'];
		if($key!= null){
			$conditions = array(
			'or'=>array(
				'Contact.name LIKE '=>'%'.$key.'%',
				'Contact.mobile LIKE '=>'%'.$key.'%',
				'Contact.address LIKE '=>'%'.$key.'%',
				'Contact.email LIKE '=>'%'.$key.'%',
				'Contact.content LIKE '=>'%'.$key.'%'
			));
		}if($stt != null){
			$conditions = array('status'=>$stt);
		}if($key!= null && $stt !=null){
			$conditions = array(
			'status'=>$stt,
			'or'=>array(
				'Contact.name LIKE '=>'%'.$key.'%',
				'Contact.mobile LIKE '=>'%'.$key.'%',
				'Contact.address LIKE '=>'%'.$key.'%',
				'Contact.email LIKE '=>'%'.$key.'%',
				'Contact.content LIKE '=>'%'.$key.'%'
			));
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Contact.id DESC'
		);
		$this->set('contacts', $this->paginate());
		$this->set(compact('key','stt'));
	}

	function admin_view($id = null) {
		if ($this->data) {
			$this->Contact->query('update tblcontacts set status = 2 where id='.$id);
			$this->redirect(array('action' => 'index'));
		}
		if (!$id) {
			$this->Session->setFlash(__('Invalid Contact', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('contact', $this->Contact->read(null, $id));
	}
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Contact', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Contact->delete($id)) {
			$this->Session->setFlash(__('Contact deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Contact was not saved. Please try again.', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Contact was not saved. Please try again.', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
			$this->Contact->delete($item);
         }
		$this->Session->setFlash(__('Contact deleted', true));
		$this->redirect(array('action' => 'index'));
    }
}

